package com.qf.threadpool;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPoolTest01 {
	
	static boolean numFlag = false;
	static boolean charFlag = false;
	static boolean flag = true;
	static int i =1;
	static char c ='A';
	public static void main(String[] args) {
		
		/*
		 * 
		 * 作业：
		创建两个线程，第一个线程输出数值  1   2   3...第二个线程输出字母  A  B  C..
		要求输出的顺序： 12A34B56C......5152Z   规律：每输出两个数值后输出一个字母
		 * 
		 * */
		Lock lock = new ReentrantLock();
		Condition withNumCondition = lock.newCondition();
		Condition withWordCondition = lock.newCondition();
		
		
		
		Thread t1 =new Thread(new Runnable() {
			
			@Override
			public void run() {
//				int i=1;
//				
//				
//					while(true) {
//						try {
//							System.out.println("数字锁开始解锁...");
//							lock.lock();
//							
//							
//							System.out.printf("%d%d",i++,i++);
//							withNumCondition.await();
//							lock.unlock();
//							System.out.println("数字锁已经被锁住...");
//							Thread.sleep(1000);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						
//					}
					
				
				
				
				
			}
			
			public void syncWord() {
				
				
				
					
				if(!flag) {
					lock.lock();
					System.out.println(c++);
					flag =true;
					lock.unlock();
				}
				else {
					try {
						withWordCondition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		});
		
		
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
//					char c = 'A';
//					while(c<'Z') {
//						
//						
//						try {
//							System.out.println("字母锁开始解锁...");
//							lock.lock();
//							
//							System.out.printf("%c",c);
//							c++;
//							withWordCondition.await();
//							lock.unlock();
//							System.out.println("字母锁已经被锁住...");
//							Thread.sleep(1000);
//							
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						
//					}
				
				
				
			}
			public void syncNum() {
				
				if(flag) {
					
					lock.lock();
					
					System.out.printf("%d%d",i++,i++);
					flag = false;
					lock.unlock();
					
				}else {
					
					
					try {
						withNumCondition.await();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
				
				
				
			}
			
			
			
		});
		
	 
		
		
		
		
		
		//lock的condition
//		Thread t4 =new Thread(new Runnable() {
//			
//			@Override
//			public void run() {
//				lock.lock();	
//				//数字
//				withNumCondition.signal();
//				
//				lock.unlock();	
//				
//			}
//		});
//		
//		t4.start();
		
		
//		for(int i=0;i<=26;i++) {
			
			//lock的condition
			Thread t3 =new Thread(new Runnable() {
				
				@Override
				public void run() {
					lock.lock();	
					
					
					withWordCondition.signal();
					lock.unlock();	
						
					
					
				}
			});
//		}
		System.out.println("t1开始运行...");
		t1.start();
		System.out.println("t2开始运行...");
		t2.start();
		
		System.out.println(withNumCondition);
	
			
				t3.start();
			
				
			
			
		
		
	}

}
